﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using YJKArchUtils.Utils;
using YJKRevitDimension.LevelDimension;
using YJKRevitPreview;

namespace YJKLevelRelated
{
	public partial class DlgLevelSet : System.Windows.Forms.Form
	{
		public DlgLevelSet(ExternalCommandData cmdData, UIApplication app)
		{
			this.m_cmdData = cmdData;
			this.m_application = app;
			this.m_docment = app.ActiveUIDocument.Document;
			this.systemLevelsDatum = new List<LevelsDataSource>();
			this.orignalLevelIds = new List<int>();
			this.newCreateLevels = new List<LevelsDataSource>();
			this.deletedLevelIds = new List<int>();
			this.modifiedLevelIds = new List<int>();
			this.InitializeComponent();
			this.InitLevelListView();
			this.m_MainPreviewer = new Previewer(1u);
			this.m_MainPreviewer.Name = "RvtCvsPreviewer";
			this.m_MainPreviewer.BackColor = System.Drawing.Color.White;
			this.m_MainPreviewer.BorderStyle = BorderStyle.FixedSingle;
			this.m_MainPreviewer.Location = this.panelPreview.Location;
			this.m_MainPreviewer.Size = this.panelPreview.Size;
			this.m_MainPreviewer.SizeMode = PictureBoxSizeMode.AutoSize;
			this.m_MainPreviewer.TabIndex = 9;
			this.m_MainPreviewer.TabStop = false;
			this.m_MainPreviewer.Paint += this.Previewer_Paint;
			this.m_MainPreviewer.InitData();
			this.panelPreview.Visible = false;
			base.Controls.Add(this.m_MainPreviewer);
		}

		private void Previewer_Paint(object sender, PaintEventArgs e)
		{
			this.RefreshPreviewItemArray();
		}

		private bool IsMove(double dElevation1, int index)
		{
			if (index == this.lvLevels.Items.Count - 1)
			{
				Convert.ToDouble(this.lvLevels.Items[index - 1].SubItems[1].Text);
			}
			return true;
		}

		private void IsMoveLevel(int index, double dElevation1, ref bool bIsMove)
		{
			try
			{
				if (index != this.lvLevels.Items.Count - 1)
				{
					double num = Convert.ToDouble(this.lvLevels.Items[index + 1].SubItems[1].Text);
					if (dElevation1 - num < 1500.0)
					{
						bIsMove = true;
						return;
					}
				}
				bIsMove = false;
			}
			catch (Exception)
			{
			}
		}

		private void MoveLevel(ref XYZ ptBase, ref XYZ ptEnd)
		{
			if (this.bIsMove && !this.bIsAlreadyMove)
			{
				ptBase += this.ptTemp;
				ptEnd += this.ptTemp;
				this.bIsAlreadyMove = true;
				return;
			}
			this.bIsAlreadyMove = false;
		}

		private void RefreshPreviewItemArray()
		{
			try
			{
				List<CurveInfo> curves = new List<CurveInfo>();
				List<TextNoteInfo> textNotes = new List<TextNoteInfo>();
				XYZ zero = XYZ.Zero;
				for (int i = this.lvLevels.Items.Count - 1; i > -1; i--)
				{
					double num = Convert.ToDouble(this.lvLevels.Items[i].SubItems[1].Text);
					this.IsMoveLevel(i, num, ref this.bIsMove);
					PenColor color = PenColor.PC_Black;
					string type = this.systemLevelsDatum.ElementAt(i).Type;
					short nType;
					if (!(type == "上标头"))
					{
						if (!(type == "下标头"))
						{
							if (!(type == "正负零标高"))
							{
								nType = 3;
							}
							else
							{
								nType = 2;
							}
						}
						else
						{
							nType = 1;
						}
					}
					else
					{
						nType = 0;
					}
					for (int j = 0; j < this.lvLevels.SelectedItems.Count; j++)
					{
						int index = this.lvLevels.SelectedItems[j].Index;
						LevelsDataSource levelsDataSource = this.systemLevelsDatum.ElementAt(index);
						if (GeoUtil.IsEqual(num, levelsDataSource.Elevation))
						{
							color = PenColor.PC_Blue;
							break;
						}
					}
					this.DrawSingleLevelInfo(nType, zero, num, color, ref curves, ref textNotes);
				}
				this.m_MainPreviewer.Curves = curves;
				this.m_MainPreviewer.TextNotes = textNotes;
			}
			catch (Exception)
			{
			}
		}

		private void DrawSingleLevelInfo(short nType, XYZ ptLeftBottom, double dElevation, PenColor color, ref List<CurveInfo> curveInfoList, ref List<TextNoteInfo> textNoteInfoList)
		{
			double num = AssistFunc.mmToFeet(3000.0);
			double num2 = AssistFunc.mmToFeet(600.0);
			XYZ xyz = ptLeftBottom + XYZ.BasisY * AssistFunc.mmToFeet(dElevation);
			XYZ xyz2 = xyz + XYZ.BasisX * num;
			this.MoveLevel(ref xyz, ref xyz2);
			Line line = Line.CreateBound(xyz - XYZ.BasisX * num2, xyz2 + XYZ.BasisX * num2);
			line.GetEndPoint(0);
			line.GetEndPoint(1);
			curveInfoList.Add(new CurveInfo(line, color, 0f));
			this.DrawSingleLevelSymbol(nType, xyz, color, dElevation, num, ref curveInfoList, ref textNoteInfoList);
		}

		private void DrawSingleLevelSymbol(short nType, XYZ ptBase, PenColor color, double dElevation, double dBaseLineLen, ref List<CurveInfo> curveInfoList, ref List<TextNoteInfo> textNoteInfoList)
		{
			double num = AssistFunc.mmToFeet(750.0);
			bool isUpward = nType != 1;
			string text = string.Format("{0:N2}", dElevation / 1000.0);
			if (nType == 2)
			{
				text = string.Format("{0:N2}", dElevation / 1000.0);
				text = this.FloorLevelAddPrefix(text);
			}
			double dTextH = num;
			this.DrawLevelSymbol(ptBase + dBaseLineLen * XYZ.BasisX, color, num, text, dTextH, false, isUpward, ref curveInfoList, ref textNoteInfoList);
		}

		private string FloorLevelAddPrefix(string strText)
		{
			if (strText.Contains("±"))
			{
				return strText;
			}
			LevelDimensionManager.StrToDouble(strText, 0.0);
			return "±" + strText;
		}

		private void DrawLevelSymbol(XYZ ptBase, PenColor color, double dLevelSymbolH, string strItem, double dTextH, bool isLeft, bool isUpward, ref List<CurveInfo> curveInfoList, ref List<TextNoteInfo> textNoteInfoList)
		{
			XYZ xyz = XYZ.BasisY;
			if (!isUpward)
			{
				xyz = -XYZ.BasisY;
			}
			XYZ xyz2 = ptBase + xyz * dLevelSymbolH;
			xyz2 -= XYZ.BasisX * dLevelSymbolH;
			XYZ xyz3 = xyz2 + XYZ.BasisX * dLevelSymbolH * 2.0;
			Line line = Line.CreateBound(ptBase, xyz2);
			line.GetEndPoint(0);
			line.GetEndPoint(1);
			Line line2 = Line.CreateBound(ptBase, xyz3);
			line2.GetEndPoint(0);
			line2.GetEndPoint(1);
			curveInfoList.Add(new CurveInfo(line, color, 0f));
			curveInfoList.Add(new CurveInfo(line2, color, 0f));
			XYZ xyz5;
			Line line3;
			if (isLeft)
			{
				XYZ xyz4 = xyz2 - XYZ.BasisX * 4.0 * dLevelSymbolH;
				xyz5 = Geometry.CalculatMidPoint(xyz4, xyz2);
				line3 = Line.CreateBound(xyz3, xyz4);
				line3.GetEndPoint(0);
				XYZ endPoint = line3.GetEndPoint(1);
				this.ptTemp = new XYZ(endPoint.X, 0.0, endPoint.Z);
			}
			else
			{
				XYZ xyz4 = xyz3 + XYZ.BasisX * 4.0 * dLevelSymbolH;
				xyz5 = Geometry.CalculatMidPoint(xyz3, xyz4);
				line3 = Line.CreateBound(xyz2, xyz4);
				line3.GetEndPoint(0);
				XYZ endPoint2 = line3.GetEndPoint(1);
				this.ptTemp = new XYZ(endPoint2.X, 0.0, endPoint2.Z);
			}
			curveInfoList.Add(new CurveInfo(line3, color, 0f));
			if (!isUpward)
			{
				xyz5 -= XYZ.BasisY * dTextH;
			}
			else
			{
				xyz5 += XYZ.BasisY * dTextH;
			}
			this.DrawLevelSymbolText(strItem, dTextH, xyz5, color, ref textNoteInfoList);
		}

		private void DrawLevelSymbolText(string strItem, double dTextH, XYZ ptTextPos, PenColor color, ref List<TextNoteInfo> textNoteInfoList)
		{
			double num = 0.0;
			double num2 = 0.0;
			WinDPIManager.GetWinFormXYDpi(ref num, ref num2);
			TextNoteInfo item = new TextNoteInfo(strItem, color, dTextH / num2, ptTextPos, dTextH * (double)strItem.Length * 1.2, dTextH * 1.2);
			item.m_AlignV = TextNoteInfo.AlignVType.AVT_Middle;
			item.m_AlignH = TextNoteInfo.AlignHType.AHT_Center;
			textNoteInfoList.Add(item);
		}

		private void LoadLevelType(LevelType levelType, string strFile, string strNewLevelTypeName)
		{
			FamilySymbol familySymbol = null;
			string text = strNewLevelTypeName;
			if (strNewLevelTypeName == "上标高标头" || strNewLevelTypeName == "下标高标头")
			{
				text = strNewLevelTypeName.Substring(0, 2) + strNewLevelTypeName.Substring(4);
			}
			LevelType elem = levelType.Duplicate(text) as LevelType;
			string appRootPath = AssistFunc.GetAppRootPath();
			string versionNumber = this.m_cmdData.Application.Application.VersionNumber;
			string text2 = appRootPath + "\\FamilyLib\\Family\\";
			text2 += versionNumber;
			text2 = text2 + "\\LevelHeadStyle\\" + strFile + ".rfa";
			try
			{
				this.m_docment.LoadFamilySymbol(text2, strNewLevelTypeName, out familySymbol);
				ElementId id = familySymbol.Id;
				elem.GetParameter("符号").Set(id);
			}
			catch
			{
			}
		}

		private void InitLevelListView()
		{
			try
			{
				IEnumerable<Element> enumerable = new FilteredElementCollector(this.m_docment).OfClass(typeof(ViewPlan)).ToElements();
				new List<string>();
				List<Level> list = new List<Level>();
				foreach (Element element in enumerable)
				{
					ViewPlan viewPlan = element as ViewPlan;
					Level level = viewPlan.GenLevel;
					if (level != null && list.Find((Level l) => l.Id == level.Id) == null)
					{
						list.Add(level);
					}
				}
				this.UnitType = RevitVersionFuncs.GetDisplayUnitType(this.m_docment, 0);
				for (int i = 0; i < list.Count; i++)
				{
					Level level2 = list.ElementAt(i);
					LevelsDataSource levelsDataSource = new LevelsDataSource();
					levelsDataSource.LevelIDValue = level2.Id.IntegerValue;
					levelsDataSource.Name = level2.Name;
					Parameter parameter = level2.get_Parameter(BuiltInParameter.LEVEL_ELEV);
					double elevation = double.Parse(Unit.CovertFromAPI(this.UnitType, parameter.AsDouble()).ToString("#.0"));
					levelsDataSource.Elevation = elevation;
					levelsDataSource.Type = level2.GetLevelType(this.m_docment).Name;
					this.systemLevelsDatum.Add(levelsDataSource);
					this.orignalLevelIds.Add(levelsDataSource.LevelIDValue);
				}
				LevelsDataComparer comparer = new LevelsDataComparer();
				this.systemLevelsDatum.Sort(comparer);
				for (int j = 0; j < this.systemLevelsDatum.Count; j++)
				{
					LevelsDataSource levelsDataSource2 = this.systemLevelsDatum.ElementAt(j);
					if (j == 0)
					{
						levelsDataSource2.LevelHeight = 0.0;
					}
					else
					{
						double levelHeight = this.systemLevelsDatum.ElementAt(j - 1).Elevation - levelsDataSource2.Elevation;
						levelsDataSource2.LevelHeight = levelHeight;
					}
				}
				this.RefreshLevelListView();
				if (this.lvLevels.Items.Count > 0)
				{
					this.lvLevels.Items[0].Selected = true;
					this.RefreshLevelInfo(this.systemLevelsDatum.ElementAt(0));
				}
			}
			catch (Exception)
			{
			}
		}

		private void RefreshLevelListView()
		{
			this.lvLevels.Items.Clear();
			for (int i = 0; i < this.systemLevelsDatum.Count; i++)
			{
				LevelsDataSource levelsDataSource = this.systemLevelsDatum.ElementAt(i);
				if (i == 0)
				{
					levelsDataSource.LevelHeight = 0.0;
				}
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = levelsDataSource.Name;
				ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = levelsDataSource.Elevation.ToString();
				listViewItem.SubItems.Add(listViewSubItem);
				ListViewItem.ListViewSubItem listViewSubItem2 = new ListViewItem.ListViewSubItem();
				listViewSubItem2.Text = levelsDataSource.LevelHeight.ToString();
				listViewItem.SubItems.Add(listViewSubItem2);
				ListViewItem.ListViewSubItem listViewSubItem3 = new ListViewItem.ListViewSubItem();
				listViewSubItem3.Text = levelsDataSource.Type;
				listViewItem.SubItems.Add(listViewSubItem3);
				this.lvLevels.Items.Add(listViewItem);
			}
			this.RefreshPreviewItemArray();
		}

		private void RefreshLevelInfo(LevelsDataSource info)
		{
			this.tbName.Text = info.Name;
			this.tbEle.Text = info.Elevation.ToString("0.0");
			this.tbHeight.Text = info.LevelHeight.ToString("0.0");
			this.cmbSymType.Text = info.Type;
		}

		public bool SetLevel(int levelIDValue, string levelName, double levelElevation, string type)
		{
			bool result;
			try
			{
				ElementId id = new ElementId(levelIDValue);
				Level level = RevitVersionFuncs.GetElement(this.m_docment, id) as Level;
				level.get_Parameter(BuiltInParameter.LEVEL_ELEV).SetValueString(levelElevation.ToString());
				level.Name = levelName;
				LevelType levelType = RevitVersionFuncs.GetElement(this.m_docment, this.GetLevelTypeId(type)) as LevelType;
				level.ChangeLevelType(levelType);
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool LevelExist(string name)
		{
			foreach (LevelsDataSource levelsDataSource in this.systemLevelsDatum)
			{
				if (name.CompareTo(levelsDataSource.Name) == 0)
				{
					return true;
				}
			}
			return false;
		}

		public void CreateLevel(string levelName, double levelElevation, string type)
		{
			Level level = this.m_docment.HyNewLevel(levelElevation);
			level.get_Parameter(BuiltInParameter.LEVEL_ELEV).SetValueString(levelElevation.ToString());
			try
			{
				level.Name = levelName;
			}
			catch (Exception)
			{
				IEnumerable<Element> enumerable = new FilteredElementCollector(this.m_docment).OfClass(typeof(Level)).ToElements();
				Level level2 = null;
				foreach (Element element in enumerable)
				{
					Level level3 = element as Level;
					if (level3.Name == levelName)
					{
						level2 = level3;
						break;
					}
				}
				this.m_docment.Delete(level2.Id);
				level.Name = levelName;
			}
			LevelType levelType = RevitVersionFuncs.GetElement(this.m_docment, this.GetLevelTypeId(type)) as LevelType;
			level.ChangeLevelType(levelType);
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_docment);
			filteredElementCollector.OfClass(typeof(LinePatternElement));
			foreach (Element element2 in filteredElementCollector)
			{
				LinePatternElement linePatternElement = element2 as LinePatternElement;
				if (linePatternElement.Name == "中心线")
				{
					ElementId id = linePatternElement.Id;
					LevelType levelType2 = level.GetLevelType(this.m_docment);
					levelType2.get_Parameter(BuiltInParameter.LINE_PATTERN).Set(id);
					levelType2.get_Parameter(BuiltInParameter.LINE_COLOR).Set(8421504);
					break;
				}
			}
			ViewPlan viewPlan = RevitVersionFuncs.NewViewPlan(this.m_docment, levelName, level, (Autodesk.Revit.DB.ViewPlanType)1);
			if (viewPlan != null)
			{
				viewPlan.get_Parameter(BuiltInParameter.VIEW_DISCIPLINE).Set(1);
				Parameter parameter = viewPlan.GetParameter("视图分类-父");
				if (parameter != null)
				{
					string text = "";
					if (this.GetPlanViewDirParent(ref text))
					{
						parameter.Set(text);
					}
				}
			}
		}

		private bool GetPlanViewDirParent(ref string strParent)
		{
			foreach (ElementId elementId in new FilteredElementCollector(this.m_docment).OfCategory(BuiltInCategory.OST_Views).OfClass(typeof(ViewPlan)).ToElementIds())
			{
				ViewPlan viewPlan = this.m_docment.GetElement(elementId) as ViewPlan;
				if (viewPlan != null && (int)viewPlan.ViewType == 1)
				{
					Parameter parameter = viewPlan.GetParameter("视图分类-父");
					if (parameter != null)
					{
						strParent = parameter.AsString();
						return true;
					}
				}
			}
			return false;
		}

		public void DeleteLevel(int IDValueOfLevel)
		{
			ElementId elementId = new ElementId(IDValueOfLevel);
			this.m_docment.Delete(elementId);
		}

		public void InitLevelType()
		{
			foreach (Element element in ElementFiltering.FindSpecialElements(this.m_docment, typeof(LevelType), BuiltInCategory.OST_Levels))
			{
				string name = (element as LevelType).Name;
				if (!this.cmbSymType.Items.Contains(name))
				{
					this.cmbSymType.Items.Add(name);
				}
			}
			if (this.cmbSymType.Items.Count > 0)
			{
				this.cmbSymType.SelectedIndex = 0;
			}
		}

		public ElementId GetLevelTypeId(string levelTypeName)
		{
			foreach (Element element in ElementFiltering.FindSpecialElements(this.m_docment, typeof(LevelType), BuiltInCategory.OST_Levels))
			{
				LevelType levelType = element as LevelType;
				if (levelType.Name.Equals(levelTypeName))
				{
					return levelType.get_Parameter(BuiltInParameter.ID_PARAM).AsElementId();
				}
			}
			return null;
		}

		private LevelsDataSource GetLevelInfoById(int id)
		{
			foreach (LevelsDataSource levelsDataSource in this.systemLevelsDatum)
			{
				if (levelsDataSource.LevelIDValue == id)
				{
					return levelsDataSource;
				}
			}
			return null;
		}

		private void btnMultAdd_Click(object sender, EventArgs e)
		{
		}

		private void btnApply_Click(object sender, EventArgs e)
		{
			if (!Assist.StringUtil.isNumberic2(this.tbHeight.Text))
			{
				YJKMessageBox.Show("楼层高度只能是正实数！");
				this.tbHeight.Focus();
				return;
			}
			double num = 0.0;
			if (!double.TryParse(this.tbEle.Text, out num))
			{
				YJKMessageBox.Show("底标高只能为实数！");
				this.tbEle.Focus();
				return;
			}
			if (this.tbName.Text == "")
			{
				YJKMessageBox.Show("楼层名不能为空！");
				this.tbName.Focus();
				return;
			}
			if (this.lvLevels.SelectedItems.Count <= 0)
			{
				return;
			}
			int index = this.lvLevels.SelectedItems[0].Index;
			for (int i = 0; i < this.systemLevelsDatum.Count; i++)
			{
				LevelsDataSource levelsDataSource = this.systemLevelsDatum.ElementAt(i);
				if (i != index && levelsDataSource.Name.CompareTo(this.tbName.Text) == 0)
				{
					YJKMessageBox.Show("楼层名已存在，请重新设置楼层名称！");
					this.tbName.Focus();
					return;
				}
			}
			LevelsDataSource levelsDataSource2 = this.systemLevelsDatum.ElementAt(index);
			levelsDataSource2.Name = this.tbName.Text;
			levelsDataSource2.Elevation = Convert.ToDouble(this.tbEle.Text);
			levelsDataSource2.LevelHeight = Convert.ToDouble(this.tbHeight.Text);
			levelsDataSource2.Type = this.cmbSymType.SelectedItem.ToString();
			if (this.orignalLevelIds.Contains(levelsDataSource2.LevelIDValue) && !this.modifiedLevelIds.Contains(levelsDataSource2.LevelIDValue))
			{
				this.modifiedLevelIds.Add(levelsDataSource2.LevelIDValue);
			}
			if (levelsDataSource2.Elevation >= 0.0)
			{
				for (int j = index - 1; j >= 0; j--)
				{
					LevelsDataSource levelsDataSource3 = this.systemLevelsDatum.ElementAt(j);
					levelsDataSource3.Elevation = levelsDataSource2.Elevation + levelsDataSource2.LevelHeight;
					if (this.orignalLevelIds.Contains(levelsDataSource3.LevelIDValue) && !this.modifiedLevelIds.Contains(levelsDataSource3.LevelIDValue))
					{
						this.modifiedLevelIds.Add(levelsDataSource3.LevelIDValue);
					}
					levelsDataSource2 = levelsDataSource3;
				}
			}
			else if (index != 0)
			{
				levelsDataSource2 = this.systemLevelsDatum.ElementAt(index - 1);
				for (int k = index; k < this.systemLevelsDatum.Count; k++)
				{
					LevelsDataSource levelsDataSource4 = this.systemLevelsDatum.ElementAt(k);
					levelsDataSource4.Elevation = levelsDataSource2.Elevation - levelsDataSource4.LevelHeight;
					if (this.orignalLevelIds.Contains(levelsDataSource4.LevelIDValue) && !this.modifiedLevelIds.Contains(levelsDataSource4.LevelIDValue))
					{
						this.modifiedLevelIds.Add(levelsDataSource4.LevelIDValue);
					}
					levelsDataSource2 = levelsDataSource4;
				}
			}
			this.RefreshLevelListView();
			this.lvLevels.Focus();
			this.lvLevels.Items[index].Selected = true;
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (this.lvLevels.SelectedItems.Count <= 0)
			{
				YJKMessageBox.Show("请先在楼层列表中选定要插入的楼层位置！");
				return;
			}
			int num = this.lvLevels.SelectedItems[0].Index;
			double srcEle = Convert.ToDouble(this.tbEle.Text);
			string strPrefix = "";
			string strSuffix = "";
			string strNewNumber = "";
			bool isChinese = false;
			this.CalculatorReferencInfo(true, ref strPrefix, ref strSuffix, ref strNewNumber, ref isChinese);
			using (DlgMultAddLevels dlgMultAddLevels = new DlgMultAddLevels(this.m_application, this.systemLevelsDatum, srcEle, 0, strPrefix, strSuffix, strNewNumber, isChinese))
			{
				if (dlgMultAddLevels.ShowDialog() == DialogResult.OK)
				{
					int num2 = num;
					if (num2 == 0)
					{
						this.systemLevelsDatum.ElementAt(0).LevelHeight = dlgMultAddLevels.newLevelsDatum.ElementAt(0).LevelHeight;
					}
					foreach (LevelsDataSource levelsDataSource in dlgMultAddLevels.newLevelsDatum)
					{
						while (this.LevelExist(levelsDataSource.Name))
						{
							LevelsDataSource levelsDataSource2 = levelsDataSource;
							levelsDataSource2.Name += "'";
						}
						this.systemLevelsDatum.Insert(num, levelsDataSource);
						this.newCreateLevels.Add(levelsDataSource);
						num++;
					}
					for (int i = 0; i < num2; i++)
					{
						LevelsDataSource levelsDataSource3 = this.systemLevelsDatum.ElementAt(i);
						levelsDataSource3.Elevation += dlgMultAddLevels.totalH;
						if (this.orignalLevelIds.Contains(levelsDataSource3.LevelIDValue) && !this.modifiedLevelIds.Contains(levelsDataSource3.LevelIDValue))
						{
							this.modifiedLevelIds.Add(levelsDataSource3.LevelIDValue);
						}
					}
					if (num2 == 0)
					{
						this.systemLevelsDatum.ElementAt(0).LevelHeight = 0.0;
					}
					this.RefreshLevelListView();
					this.lvLevels.Focus();
					this.lvLevels.Items[num].Selected = true;
					this.m_MainPreviewer.Refresh();
				}
			}
		}

		private void btnAddDown_Click(object sender, EventArgs e)
		{
			if (this.lvLevels.SelectedItems.Count <= 0)
			{
				YJKMessageBox.Show("请先在楼层列表中选定要插入的楼层位置！");
				return;
			}
			int index = this.lvLevels.SelectedItems[0].Index;
			int num = index + 1;
			double srcEle = Convert.ToDouble(this.tbEle.Text);
			string strPrefix = "";
			string strSuffix = "";
			string strNewNumber = "";
			bool isChinese = false;
			this.CalculatorReferencInfo(false, ref strPrefix, ref strSuffix, ref strNewNumber, ref isChinese);
			using (DlgMultAddLevels dlgMultAddLevels = new DlgMultAddLevels(this.m_application, this.systemLevelsDatum, srcEle, 1, strPrefix, strSuffix, strNewNumber, isChinese))
			{
				if (dlgMultAddLevels.ShowDialog() == DialogResult.OK)
				{
					bool flag = index == this.lvLevels.Items.Count - 1;
					foreach (LevelsDataSource levelsDataSource in dlgMultAddLevels.newLevelsDatum)
					{
						while (this.LevelExist(levelsDataSource.Name))
						{
							LevelsDataSource levelsDataSource2 = levelsDataSource;
							levelsDataSource2.Name += "'";
						}
						if (flag)
						{
							this.systemLevelsDatum.Add(levelsDataSource);
						}
						else
						{
							this.systemLevelsDatum.Insert(num, levelsDataSource);
						}
						this.newCreateLevels.Add(levelsDataSource);
						num++;
					}
					for (int i = num; i < this.systemLevelsDatum.Count; i++)
					{
						LevelsDataSource levelsDataSource3 = this.systemLevelsDatum.ElementAt(i);
						levelsDataSource3.Elevation -= dlgMultAddLevels.totalH;
						if (this.orignalLevelIds.Contains(levelsDataSource3.LevelIDValue) && !this.modifiedLevelIds.Contains(levelsDataSource3.LevelIDValue))
						{
							this.modifiedLevelIds.Add(levelsDataSource3.LevelIDValue);
						}
					}
					this.RefreshLevelListView();
					this.lvLevels.Focus();
					this.lvLevels.Items[index].Selected = true;
					this.m_MainPreviewer.Refresh();
				}
			}
		}

		private void CalculatorReferencInfo(bool isUpward, ref string strPrefix, ref string strSuffix, ref string strNewNumber, ref bool isChinese)
		{
			string text = this.tbName.Text;
			string text2 = "0123456789";
			string text3 = "零一二三四五六七八九十百千万亿";
			int i = 0;
			int num = -1;
			int num2 = -1;
			while (i < text.Length)
			{
				if (text2.IndexOf(text[i]) != -1)
				{
					if (num == -1)
					{
						num = i;
						num2 = i;
					}
					else
					{
						num2 = i;
					}
				}
				i++;
			}
			DlgLevelSet.LevelNumberType numType = DlgLevelSet.LevelNumberType.LNT_Arabic;
			if (num == -1 || num2 == -1)
			{
				for (i = 0; i < text.Length; i++)
				{
					if (text3.IndexOf(text[i]) != -1)
					{
						if (num == -1)
						{
							num = i;
							num2 = i;
						}
						else
						{
							num2 = i;
						}
					}
				}
				numType = DlgLevelSet.LevelNumberType.LNT_Chinese;
			}
			if (num == -1 || num2 == -1)
			{
				int num3 = text.IndexOf("首层");
				if (num3 != -1)
				{
					text = text.Replace("首", "1");
				}
				num = num3;
				num2 = num3;
				numType = DlgLevelSet.LevelNumberType.LNT_Arabic;
			}
			if (num != -1 || num2 != -1)
			{
				this.CalculatorReferencInfo(text, num, num2, isUpward, numType, ref strPrefix, ref strSuffix, ref strNewNumber, ref isChinese);
			}
		}

		private bool CalculatorReferencInfo(string currentName, int startIndex, int endIndex, bool isUpward, DlgLevelSet.LevelNumberType numType, ref string strRealPrefix, ref string strRealSuffix, ref string strRealNewNumber, ref bool isChinese)
		{
			bool result;
			try
			{
				int num = 1;
				if (!isUpward)
				{
					num = -1;
				}
				string text = currentName.Substring(0, startIndex);
				int num2 = 1;
				if (text.IndexOf("负") != -1)
				{
					num2 = -1;
					text = text.Substring(0, text.IndexOf("负"));
				}
				else if (text.IndexOf("B") != -1)
				{
					num2 = -1;
					text = text.Substring(0, text.IndexOf("B"));
				}
				else if (text.IndexOf("地下") != -1)
				{
					num2 = -1;
					text = text.Substring(0, text.IndexOf("地下"));
				}
				string text2 = currentName.Substring(startIndex, endIndex - startIndex + 1);
				if (numType == DlgLevelSet.LevelNumberType.LNT_Arabic)
				{
					isChinese = false;
				}
				else if (numType == DlgLevelSet.LevelNumberType.LNT_Chinese)
				{
					isChinese = true;
					text2 = ArabicNumeralsConvertChineseDigitalKit.ChnToNum(text2).ToString();
				}
				int num3 = Convert.ToInt32(text2);
				num3 = num3 * num2 + num;
				if (num3 == 0)
				{
					if (isUpward)
					{
						num3 = 1;
					}
					else
					{
						num3 = -1;
					}
				}
				strRealNewNumber = num3.ToString();
				string text3 = currentName.Substring(endIndex + 1);
				strRealPrefix = text;
				strRealSuffix = text3;
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private void btnMultModify_Click(object sender, EventArgs e)
		{
			List<LevelsDataSource> list = new List<LevelsDataSource>();
			for (int i = 0; i < this.lvLevels.SelectedItems.Count; i++)
			{
				int index = this.lvLevels.SelectedItems[i].Index;
				LevelsDataSource item = this.systemLevelsDatum.ElementAt(index);
				list.Add(item);
			}
			using (DlgMultModify dlgMultModify = new DlgMultModify(this.m_application, ref list))
			{
				if (dlgMultModify.ShowDialog() == DialogResult.OK)
				{
					for (int j = 0; j < list.Count; j++)
					{
						LevelsDataSource levelsDataSource = list.ElementAt(j);
						if (this.orignalLevelIds.Contains(levelsDataSource.LevelIDValue) && !this.modifiedLevelIds.Contains(levelsDataSource.LevelIDValue))
						{
							this.modifiedLevelIds.Add(levelsDataSource.LevelIDValue);
						}
						int num = 0;
						int k = 0;
						while (k < this.systemLevelsDatum.Count)
						{
							if (levelsDataSource.IsEqual(this.systemLevelsDatum.ElementAt(k)))
							{
								num = k;
								if (num == 0)
								{
									levelsDataSource.LevelHeight = 0.0;
									break;
								}
								break;
							}
							else
							{
								k++;
							}
						}
						if (dlgMultModify.type == 0)
						{
							for (int l = num - 1; l >= 0; l--)
							{
								LevelsDataSource levelsDataSource2 = this.systemLevelsDatum.ElementAt(l);
								levelsDataSource2.Elevation = levelsDataSource.Elevation + levelsDataSource.LevelHeight;
								if (this.orignalLevelIds.Contains(levelsDataSource2.LevelIDValue) && !this.modifiedLevelIds.Contains(levelsDataSource2.LevelIDValue))
								{
									this.modifiedLevelIds.Add(levelsDataSource2.LevelIDValue);
								}
								levelsDataSource = levelsDataSource2;
							}
						}
						else if (dlgMultModify.type == 1)
						{
							for (int m = num + 1; m < this.systemLevelsDatum.Count; m++)
							{
								LevelsDataSource levelsDataSource3 = this.systemLevelsDatum.ElementAt(m);
								levelsDataSource3.Elevation = levelsDataSource.Elevation - levelsDataSource3.LevelHeight;
								if (this.orignalLevelIds.Contains(levelsDataSource3.LevelIDValue) && !this.modifiedLevelIds.Contains(levelsDataSource3.LevelIDValue))
								{
									this.modifiedLevelIds.Add(levelsDataSource3.LevelIDValue);
								}
								levelsDataSource = levelsDataSource3;
							}
						}
					}
					this.RefreshLevelListView();
					this.lvLevels.Focus();
					this.lvLevels.Items[0].Selected = true;
					this.tbEle.Enabled = true;
					this.tbHeight.Enabled = false;
					this.tbName.Enabled = true;
					this.cmbSymType.Enabled = true;
					this.btnApply.Enabled = true;
					this.btnAdd.Enabled = true;
					this.btnAddDown.Enabled = true;
					this.btnMultModify.Enabled = false;
					this.m_MainPreviewer.Refresh();
				}
			}
		}

		private void HandlePositive(int index)
		{
			bool flag = false;
			for (int i = index - 1; i >= 0; i--)
			{
				LevelsDataSource levelsDataSource = this.systemLevelsDatum.ElementAt(i);
				if (!flag)
				{
					levelsDataSource.Elevation = this.systemLevelsDatum.ElementAt(index).Elevation;
					flag = true;
				}
				else
				{
					LevelsDataSource levelsDataSource2 = this.systemLevelsDatum.ElementAt(i + 1);
					levelsDataSource.Elevation = levelsDataSource2.Elevation + levelsDataSource2.LevelHeight;
				}
			}
		}

		private void HandleNegative(int index)
		{
			if (index == 0)
			{
				return;
			}
			int num = 0;
			for (int i = index + 1; i <= this.systemLevelsDatum.Count - 1; i++)
			{
				LevelsDataSource levelsDataSource = this.systemLevelsDatum.ElementAt(i);
				if (num == 0)
				{
					LevelsDataSource levelsDataSource2 = this.systemLevelsDatum.ElementAt(i - 2);
					levelsDataSource.Elevation = levelsDataSource2.Elevation - levelsDataSource.LevelHeight;
					num = 1;
				}
				else
				{
					levelsDataSource.Elevation = this.systemLevelsDatum.ElementAt(i - 1).Elevation - levelsDataSource.LevelHeight;
				}
				if (this.orignalLevelIds.Contains(levelsDataSource.LevelIDValue) && !this.modifiedLevelIds.Contains(levelsDataSource.LevelIDValue))
				{
					this.modifiedLevelIds.Add(levelsDataSource.LevelIDValue);
				}
			}
		}

		private void HandleZero(int index)
		{
			LevelsDataSource levelsDataSource = null;
			if (index == 0)
			{
				levelsDataSource = this.systemLevelsDatum.ElementAt(1);
				levelsDataSource.Elevation += levelsDataSource.LevelHeight;
				levelsDataSource.LevelHeight = 0.0;
				for (int i = index + 2; i < this.systemLevelsDatum.Count; i++)
				{
					levelsDataSource = this.systemLevelsDatum.ElementAt(i);
					levelsDataSource.Elevation = this.systemLevelsDatum.ElementAt(i - 1).Elevation - levelsDataSource.LevelHeight;
				}
			}
			else
			{
				for (int j = index - 1; j >= 0; j--)
				{
					levelsDataSource = this.systemLevelsDatum.ElementAt(j);
					levelsDataSource.Elevation -= this.systemLevelsDatum.ElementAt(j + 1).LevelHeight;
					levelsDataSource.LevelHeight = levelsDataSource.Elevation - this.systemLevelsDatum.ElementAt(j + 1).Elevation;
				}
			}
			if (this.orignalLevelIds.Contains(levelsDataSource.LevelIDValue) && !this.modifiedLevelIds.Contains(levelsDataSource.LevelIDValue))
			{
				this.modifiedLevelIds.Add(levelsDataSource.LevelIDValue);
			}
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			if (this.lvLevels.SelectedItems.Count <= 0)
			{
				YJKMessageBox.Show("请先在楼层列表中选定要删除的楼层！");
				return;
			}
			if (this.lvLevels.SelectedItems.Count == this.systemLevelsDatum.Count)
			{
				YJKMessageBox.Show("至少保留一条楼层信息");
				return;
			}
			if (1 == this.systemLevelsDatum.Count)
			{
				YJKMessageBox.Show("不允许删除唯一的楼层！");
				return;
			}
			int num = 0;
			bool flag = false;
			for (int i = 0; i < this.lvLevels.SelectedItems.Count; i++)
			{
				num = this.lvLevels.SelectedItems[i].Index;
				if (flag && this.lvLevels.SelectedItems.Count > 1)
				{
					num++;
				}
				int num2 = num;
				num -= i;
				LevelsDataSource levelsDataSource = this.systemLevelsDatum.ElementAt(num);
				if ((int)AssistFunc.GetCurrentViewType(this.m_docment) == 1 && levelsDataSource.LevelIDValue == AssistFunc.GetCurrentViewLevel(this.m_docment).Id.IntegerValue)
				{
					YJKMessageBox.Show("不允许删除当前打开的楼层！");
					flag = true;
				}
				else
				{
					if (this.newCreateLevels.Contains(levelsDataSource))
					{
						this.newCreateLevels.Remove(levelsDataSource);
					}
					if (Geometry.LessThan(levelsDataSource.Elevation, 0.0))
					{
						this.HandleNegative(num);
					}
					else
					{
						this.HandlePositive(num);
					}
					this.systemLevelsDatum.RemoveAt(num2 - i);
					if (this.orignalLevelIds.Contains(levelsDataSource.LevelIDValue))
					{
						this.deletedLevelIds.Add(levelsDataSource.LevelIDValue);
					}
				}
			}
			this.RefreshLevelListView();
			this.lvLevels.Focus();
			int num3 = (num == 0) ? 0 : (num - 1);
			if (num3 >= this.lvLevels.Items.Count)
			{
				num3 = this.lvLevels.Items.Count - 1;
			}
			this.lvLevels.Items[num3].Selected = true;
			if (num3 == 0)
			{
				this.tbHeight.Enabled = false;
			}
			else
			{
				this.tbHeight.Enabled = true;
			}
			this.tbEle.Enabled = true;
			this.tbName.Enabled = true;
			this.cmbSymType.Enabled = true;
			this.btnApply.Enabled = true;
			this.btnAdd.Enabled = true;
			this.btnAddDown.Enabled = true;
			this.btnMultModify.Enabled = false;
			this.m_MainPreviewer.Refresh();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			foreach (int idvalueOfLevel in this.deletedLevelIds)
			{
				this.DeleteLevel(idvalueOfLevel);
			}
			List<LevelsDataSource> list = new List<LevelsDataSource>();
			foreach (int id in this.modifiedLevelIds)
			{
				LevelsDataSource levelInfoById = this.GetLevelInfoById(id);
				if (levelInfoById != null && !this.SetLevel(levelInfoById.LevelIDValue, levelInfoById.Name, levelInfoById.Elevation, levelInfoById.Type))
				{
					levelInfoById.Name = "TempName" + levelInfoById.Name;
					list.Add(levelInfoById);
					this.SetLevel(levelInfoById.LevelIDValue, levelInfoById.Name, levelInfoById.Elevation, levelInfoById.Type);
				}
			}
			foreach (LevelsDataSource levelsDataSource in list)
			{
				levelsDataSource.Name = levelsDataSource.Name.Remove(0, 8);
				this.SetLevel(levelsDataSource.LevelIDValue, levelsDataSource.Name, levelsDataSource.Elevation, levelsDataSource.Type);
			}
			foreach (LevelsDataSource levelsDataSource2 in this.newCreateLevels)
			{
				this.CreateLevel(levelsDataSource2.Name, levelsDataSource2.Elevation, levelsDataSource2.Type);
			}
			this.ModifyLevelLengh();
		}

		private void ModifyLevelLengh()
		{
			try
			{
				ICollection<Element> collection = new FilteredElementCollector(this.m_docment).OfClass(typeof(Level)).ToElements();
				Level levelOld = collection.ElementAt(0) as Level;
				foreach (Element element in new FilteredElementCollector(this.m_docment).OfClass(typeof(ViewSection)).ToElements())
				{
					ViewSection viewSection = element as ViewSection;
					if (viewSection.Name == "出图-东" || viewSection.Name == "出图-南" || viewSection.Name == "出图-西" || viewSection.Name == "出图-北")
					{
						this.ModifyLevelLengh(viewSection, levelOld, collection);
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private void ModifyLevelLengh(ViewSection viewSection, Level levelOld, ICollection<Element> collection)
		{
			XYZ xyz = XYZ.Zero;
			XYZ xyz2 = XYZ.Zero;
			IList<Curve> curvesInView = levelOld.GetCurvesInView(0, viewSection);
			if (curvesInView.Count != 0)
			{
				xyz = curvesInView[0].GetEndPoint(0);
				xyz2 = curvesInView[0].GetEndPoint(1);
			}
			foreach (Element element in collection)
			{
				if (element is Level)
				{
					Level level = element as Level;
                    IList<Curve> curvesInView2 = level.GetCurvesInView((DatumExtentType)1, viewSection);
					if (curvesInView2.Count != 0)
					{
						XYZ endPoint = curvesInView2[0].GetEndPoint(0);
						XYZ endPoint2 = curvesInView2[0].GetEndPoint(1);
						XYZ xyz3 = XYZ.Zero;
						XYZ xyz4 = XYZ.Zero;
						if (viewSection.Name == "出图-东")
						{
							xyz3 = endPoint + viewSection.RightDirection * (xyz.Y - endPoint.Y);
							xyz4 = endPoint2 + viewSection.RightDirection * (xyz2.Y - endPoint2.Y);
						}
						else if (viewSection.Name == "出图-北")
						{
							xyz3 = endPoint - viewSection.RightDirection * (xyz.X - endPoint.X);
							xyz4 = endPoint2 - viewSection.RightDirection * (xyz2.X - endPoint2.X);
						}
						else if (viewSection.Name == "出图-南")
						{
							xyz3 = endPoint + viewSection.RightDirection * (xyz.X - endPoint.X);
							xyz4 = endPoint2 + viewSection.RightDirection * (xyz2.X - endPoint2.X);
						}
						else
						{
							xyz3 = endPoint - viewSection.RightDirection * (xyz.Y - endPoint.Y);
							xyz4 = endPoint2 - viewSection.RightDirection * (xyz2.Y - endPoint2.Y);
						}
						Curve curve = Line.CreateBound(xyz3, xyz4);
                        level.SetCurveInView((DatumExtentType)1, viewSection, curve);
					}
				}
			}
		}

		private void lvLevels_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
		{
			int itemIndex = e.ItemIndex;
			LevelsDataSource info = this.systemLevelsDatum.ElementAt(itemIndex);
			this.RefreshLevelInfo(info);
			if (itemIndex == 0)
			{
				this.tbHeight.ReadOnly = true;
			}
			else
			{
				this.tbHeight.ReadOnly = false;
			}
			this.RefreshPreviewItemArray();
			this.m_MainPreviewer.Refresh();
		}

		private void tbName_Leave(object sender, EventArgs e)
		{
		}

		private void rbSetEle_CheckedChanged(object sender, EventArgs e)
		{
			this.tbEle.ReadOnly = false;
			this.tbHeight.ReadOnly = true;
		}

		private void rbSetHeight_CheckedChanged(object sender, EventArgs e)
		{
			this.tbEle.ReadOnly = true;
			this.tbHeight.ReadOnly = false;
		}

		private void tbHeight_Leave(object sender, EventArgs e)
		{
		}

		private void tbEle_Leave(object sender, EventArgs e)
		{
		}

		private void lvLevels_MouseDown(object sender, MouseEventArgs e)
		{
			this.arSelectedIndex = new List<int>();
			if (this.lvLevels.SelectedItems.Count > 0)
			{
				this.arSelectedIndex.Clear();
				for (int i = 0; i < this.lvLevels.SelectedItems.Count; i++)
				{
					int index = this.lvLevels.SelectedItems[i].Index;
					this.arSelectedIndex.Add(index);
				}
			}
		}

		private void lvLevels_MouseUp(object sender, MouseEventArgs e)
		{
			if (this.lvLevels.SelectedItems.Count == 0)
			{
				for (int i = 0; i < this.arSelectedIndex.Count; i++)
				{
					int index = this.arSelectedIndex.ElementAt(i);
					this.lvLevels.Items[index].Selected = true;
				}
				return;
			}
			if (this.lvLevels.SelectedItems.Count == 1)
			{
				if (this.lvLevels.SelectedItems[0].Index == 0)
				{
					this.tbHeight.Enabled = false;
				}
				else
				{
					this.tbHeight.Enabled = true;
				}
				this.tbEle.Enabled = true;
				this.tbName.Enabled = true;
				this.cmbSymType.Enabled = true;
				this.btnApply.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnAddDown.Enabled = true;
				this.btnMultModify.Enabled = false;
				return;
			}
			this.tbEle.Enabled = false;
			this.tbHeight.Enabled = false;
			this.tbName.Enabled = false;
			this.cmbSymType.Enabled = false;
			this.btnApply.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnAddDown.Enabled = false;
			this.btnMultModify.Enabled = true;
		}

		private void DlgLevelSet_Load(object sender, EventArgs e)
		{
			LevelType levelType = null;
			new List<Level>();
			ICollection<Element> collection = new FilteredElementCollector(this.m_docment).OfClass(typeof(LevelType)).ToElements();
			IEnumerator<Element> enumerator = collection.GetEnumerator();
			{
				if (enumerator.MoveNext())
				{
					levelType = (LevelType)enumerator.Current;
				}
			}
			bool flag = collection.Contains((Element k) => k.Name == "上标头");
			bool flag2 = collection.Contains((Element k) => k.Name == "正负零标高");
			bool flag3 = collection.Contains((Element k) => k.Name == "下标头");
			if (!flag)
			{
				this.LoadLevelType(levelType, "标记-上标高标头", "上标高标头");
			}
			if (!flag2)
			{
				this.LoadLevelType(levelType, "标记-正负零标高", "正负零标高");
			}
			if (!flag3)
			{
				this.LoadLevelType(levelType, "标记-下标高标头", "下标高标头");
			}
			this.InitLevelType();
		}

		private ExternalCommandData m_cmdData;

		private Previewer m_MainPreviewer;

		private UIApplication m_application;

		private Document m_docment;

		private DisplayUnitType UnitType;

		private List<LevelsDataSource> systemLevelsDatum;

		private List<int> orignalLevelIds;

		private List<LevelsDataSource> newCreateLevels;

		private List<int> deletedLevelIds;

		private List<int> modifiedLevelIds;

		private XYZ ptTemp = XYZ.Zero;

		private bool bIsAlreadyMove;

		private bool bIsMove;

		private List<int> arSelectedIndex;

		private enum LevelNumberType
		{
			LNT_Invalid = -1,
			LNT_Arabic,
			LNT_Chinese
		}
	}
}
